package com.qys.livesMall.shop.controller.app;

import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.kafka.entity.shop.ShopBrowseRecord;
import com.qys.livesMall.kafka.entity.shop.ShopGoodsInterestInfo;
import com.qys.livesMall.kafka.entity.shop.ShopInterestInfo;
import com.qys.livesMall.kafka.send.KafkaClient;
import com.qys.livesMall.shop.modal.dto.shop.record.ShopRecordSearch;
import com.qys.livesMall.shop.modal.vo.ShopBrowseRecordVO;
import com.qys.livesMall.shop.modal.vo.ShopGoodsInterestVO;
import com.qys.livesMall.shop.modal.vo.ShopUserInterestVO;
import com.qys.livesMall.shop.service.ShopRecordService;
import com.qys.livesMall.shop.service.UserProductPreferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * APP店铺浏览关注相关
 * @date 2023/06/03 14:56
 */
@RestController
@RequestMapping("/app/shop")
public class AppH2ShopInterestController {



    @Autowired
    private KafkaClient kafkaClient;

    @Autowired
    private ShopRecordService shopRecordService;

    @Autowired
    private UserProductPreferenceService userProductPreferenceService;

    /**
     * 浏览店铺
     * @param shopId
     * @return
     */
    @PostMapping(value = "/browseShop/{shopId}")
    public R<?> browseShop(@PathVariable Long shopId) {
        ShopBrowseRecord record=new ShopBrowseRecord();
        record.setShopId(shopId);
        record.setUserId(SecurityUtils.getUserId());
        record.setBrowseDate(LocalDateTime.now());
        kafkaClient.shopBrowseRecord(record);
        return R.ok();
    }

    /**
     * 店铺浏览记录
     * @param search
     * @return
     */
    @PostMapping(value = "/browseShop/page")
    public R<QysPage<ShopBrowseRecordVO>> browseShopPage(@RequestBody ShopRecordSearch search) {
        QysPage<ShopBrowseRecordVO> page=shopRecordService.searchShopBrowse(search);
        return R.ok(page);
    }

    /**
     * 关注店铺
     * @param shopId
     * @return
     */
    @PostMapping(value = "/interestShop/{shopId}")
    public R<?> interestShop(@PathVariable Long shopId) {
        ShopInterestInfo interestInfo=new ShopInterestInfo();
        interestInfo.setShopId(shopId);
        interestInfo.setUserId(SecurityUtils.getUserId());
        interestInfo.setBrowseDate(LocalDateTime.now());
        shopRecordService.interest(interestInfo);
        return R.ok();
    }

    /**
     * 取消关注店铺
     * @param shopId
     * @return
     */
    @PostMapping(value = "/interestShopCancel/{shopId}")
    public R<?> interestShopCancel(@PathVariable Long shopId) {
        shopRecordService.interestCancel(shopId,SecurityUtils.getUserId());
        return R.ok();
    }

    /**
     * 个人店铺关注记录
     * @param search
     * @return
     */
    @PostMapping(value = "/interestShop/page")
    public R<QysPage<ShopUserInterestVO>> interestShopPage(@RequestBody ShopRecordSearch search) {
        QysPage<ShopUserInterestVO> page=shopRecordService.searchUserInterestShop(search);
        return R.ok(page);
    }


    /**
     * 是否已经关注店铺
     * @param shopId
     * @return 0:否，1：是
     */
    @GetMapping(value = "/isInterestShop/{shopId}")
    public R<Integer> isInterestShop(@PathVariable Long shopId) {
        return R.ok(shopRecordService.isInterestShop(shopId));
    }

    /**
     * 是否已经收藏商品
     * @param shopGoodsId
     * @return
     */
    @PostMapping(value = "/isInterestShopGoods/{shopGoodsId}")
    public R<Integer> isInterestShopGoods(@PathVariable Long  shopGoodsId) {
        return R.ok(shopRecordService.isInterestShopGoods(shopGoodsId));
    }


    /**
     * 个人商品收藏记录
     * @param search
     * @return
     */
    @PostMapping(value = "/interestShopGoods/page")
    public R<QysPage<ShopGoodsInterestVO>> interestShopGoodsPage(@RequestBody ShopRecordSearch search) {
        QysPage<ShopGoodsInterestVO> page=shopRecordService.searchUserInterestShopGoods(search);
        //todo
        return R.ok(page);
    }

    /**
     * 收藏商品
     * @param shopId
     * @return
     */
    @PostMapping(value = "/interestShopGoods/{shopId}/{shopGoodsId}")
    public R<?> interestShop(@PathVariable Long shopId,@PathVariable Long shopGoodsId) {
        ShopGoodsInterestInfo interestInfo=new ShopGoodsInterestInfo();
        interestInfo.setShopId(shopId);
        interestInfo.setUserId(SecurityUtils.getUserId());
        interestInfo.setShopGoodsId(shopGoodsId);
        interestInfo.setBrowseDate(LocalDateTime.now());
        shopRecordService.interestShopGoods(interestInfo);
        return R.ok();
    }

    /**
     * 取消收藏商品
     * @param shopGoodsId
     * @return
     */
    @PostMapping(value = "/interestShopGoodsCancel/{shopGoodsId}")
    public R<?> interestShopGoodsCancel(@PathVariable Long shopGoodsId) {
        shopRecordService.shopGoodsInterestCancel(shopGoodsId);
        return R.ok();
    }

    /**
     * 浏览店铺商品（记录偏好）
     * @param productCategoryId
     * @return
     */
    @PostMapping(value = "/browseProducts/{productCategoryId}")
    public R<?> browseProducts(@PathVariable Long productCategoryId) {
        userProductPreferenceService.updatePreference(productCategoryId);
        return R.ok();
    }

}

